<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Fenxiao_Finance extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
    }

    public function action_index()
    {
        $action = $this->params['action'];
        if (empty($action))  //显示线路列表页
        {
            $group_list = DB::select()->from('fenxiao_group')->execute()->as_array();
            $rank_list = DB::select()->from('fenxiao_rank')->execute()->as_array();

            $this->assign('group_list',$group_list);
            $this->assign('rank_list',$rank_list);
            $this->display('admin/fenxiao/finance/index');
        }
        else if ($action == 'read')    //读取列表
        {
            $start = Arr::get($_GET, 'start');
            $limit = Arr::get($_GET, 'limit');
            $keyword = Arr::get($_GET, 'keyword');
            $status = $_GET['status'];
            $sort = json_decode(Arr::get($_GET, 'sort'), true);
            //$order = 'order by a.fxjointime desc';
            $typeid = $_GET['typeid'];
            $commission_status = $_GET['commission_status'];
            //$keyword = Common::getKeyword($keyword);
            if ($sort[0]['property'])
            {
                $prefix = '';
                if ($sort[0]['property'] == 'fxjointime')
                {
                    $prefix = 'a.';
                }
                $order = 'order by ' . $prefix . $sort[0]['property'] . ' ' . $sort[0]['direction'] . ',a.fxjointime desc';

            }
            $w = " where a.id is not null and (b.status=2 || b.status=5) ";

            if(!empty($commission_status) || $this->is_zero($commission_status))
            {
                if($this->is_zero($commission_status))
                {
                    $w.=" and b.status!=5 ";
                }
                else
                {
                    $w.=" and b.status!=5 ";
                }
            }
            if(!empty($typeid))
            {
                $w.=" and b.typeid={$typeid} ";
            }
            if(!empty($keyword)){
                $w.="and b.ordersn='{$keyword}'";
            }


            $sql = "select a.*,b.ordersn,b.status from  sline_fenxiao_data a inner join sline_member_order b on a.order_id=b.id  $w $order limit {$start},{$limit}";
            $totalcount_arr = DB::query(Database::SELECT, "select count(*) as num from  sline_fenxiao_data a inner join sline_member_order b on a.order_id=b.id  $w")->execute()->as_array();
            $list = DB::query(Database::SELECT, $sql)->execute()->as_array();
            foreach($list as &$row)
            {
                $row['addtime'] = !empty($row['addtime'])?date('Y-m-d H:i',$row['addtime']):'';

                $order_info = Model_Member_Order::order_info($row['ordersn']);
                $row['productname'] = $order_info['productname'];


                $member_info = Model_Member::get_member_byid($row['memberid']);
                $row['member_name'] = $member_info['nickname'];

                $member1_info = Model_Member::get_member_byid($row['memberid1']);
                $row['member1_name'] = $member1_info['nickname'];

                $member2_info = Model_Member::get_member_byid($row['memberid2']);
                $row['member2_name'] = $member2_info['nickname'];

                $member3_info = Model_Member::get_member_byid($row['memberid3']);
                $row['member3_name'] = $member3_info['nickname'];



                if($row['status']!='5')
                {
                    $order_temp = Model_Member_Order::order_info($row['ordersn']);
                    $commission_info = Model_Fenxiao::get_order_fenxiao_info($order_temp);
                    $row['commission'] = $commission_info['commission'];
                    $row['commission1'] = $commission_info['commission1'];
                    $row['commission2'] = $commission_info['commission2'];
                    $row['commission3'] = $commission_info['commission3'];
                }
                else
                {
                    $row['commission']  = DB::select()->from('fenxiao_record')->where('memberid','=',$row['memberid'])->and_where('fxmemberid','=',$row['memberid'])->and_where('orderid','=',$row['order_id'])->and_where('type','=',0)->execute()->get('amount');
                    $row['commission1']  =!empty($row['memberid1'])? DB::select()->from('fenxiao_record')->where('memberid','=',$row['memberid1'])->and_where('fxmemberid','=',$row['memberid'])->and_where('orderid','=',$row['order_id'])->and_where('type','=',0)->execute()->get('amount'):0;
                    $row['commission2']  =!empty($row['memberid2'])? DB::select()->from('fenxiao_record')->where('memberid','=',$row['memberid2'])->and_where('fxmemberid','=',$row['memberid'])->and_where('orderid','=',$row['order_id'])->and_where('type','=',0)->execute()->get('amount'):0;
                    $row['commission3']  =!empty($row['memberid3'])? DB::select()->from('fenxiao_record')->where('memberid','=',$row['memberid3'])->and_where('fxmemberid','=',$row['memberid'])->and_where('orderid','=',$row['order_id'])->and_where('type','=',0)->execute()->get('amount'):0;

                }
            }
            $result['total'] = $totalcount_arr[0]['num'];
            $result['lists'] = $list;
            $result['success'] = true;
            echo json_encode($result);
        }
        else if ($action == 'save')   //保存字段
        {
        }
        else if ($action == 'delete') //删除某个记录
        {

        }
        else if ($action == 'update')//更新某个字段
        {
            $id=Arr::get($_POST,'id');
            $field=Arr::get($_POST,'field');
            $val=Arr::get($_POST,'val');
            $model=ORM::factory('fenxiao')->where('memberid','=',$id)->find();
            $curtime=time();
            if($model->id)
            {
                $model->$field=$val;
                if($field=='isfrozen')
                {
                    if($val==1)
                        $model->frozentime=$curtime;
                    else
                    {
                        $model->unfrozentime=$curtime;
                        $model->fxlogintime=$curtime;
                    }
                }
                $model->save();
                if($model->saved())
                    echo 'ok';
                else
                    echo 'no';
            }

        }

    }
    //判断某数是不是为0
    private function is_zero($val)
    {
        if($val===0 || $val==='0')
        {
            return true;
        }
        return false;
    }

}
